(function(){
	/**
	 * VideoWall.
	 * @namespace
	 */
	 
	var TIME_FOR_SEEK = 5 * 60;
	var CHANNELING = 5 * 60;
	var contents = [
		"../TOP.mp4", "../ENEWS_EP1673.mp4"
	];
	
	function timeFormat(sec) {
		var hour = parseInt(sec/3600,10),
			mins = parseInt(sec/60,10),
			secs = parseInt(sec - mins*60,10),
		hour = (hour > 9 ? hour : '0' + hour);
		mins = (mins > 9 ? mins : '0' + mins);
		secs = (secs > 9 ? secs : '0' + secs);
		return hour == '00' ? mins + ":" + secs : hour + mins;
	}
	
	var HTML5Video = {
		init: function(){
			this.player = this.getVideo();
			return this;
		},
		handleEvent: function(evt) {
			switch (evt.type){
				case 'timeupdate':
					this.playing(evt);
					break;
				case 'play':
					break;
				case 'pause':
					break;
				case 'ended':
					break;
			}
		},
		bindEvent: function(){
			this.unbindEvent();
			this.player.addEventListener('timeupdate',  this, false);
			this.player.addEventListener('play', this, false);
			this.player.addEventListener('pause', this, false);
			this.player.addEventListener('ended', this, false);
		},
		unbindEvent: function(){
			this.player.removeEventListener('timeupdate',  this, false);
			this.player.removeEventListener('play', this, false);
			this.player.removeEventListener('pause', this, false);
			this.player.removeEventListener('ended', this, false);
		},
		getVideo: function(){
			var player;
			$('#videoHolder').html('<video width="1280" height="720" id="video" muted></object>');
			player = $('#video')[0];
			return player;
		},
		removeVideo: function(){
			this.stop();
			$('#videoHolder').html('');
		},
		playing: function(evt){
			try{
				var player = evt.target,
				total = parseInt(player.duration, 10),
				rem = parseInt(player.currentTime, 10),//parseInt(total - player.currentTime, 10),
				pos = (player.currentTime / player.duration) * 100;
				if (pos > 100) pos = 100;
				$('#vod .bar b').css('width', pos + '%');
				$('#remainingTime').text(timeFormat(player.currentTime  || 0));
				$('#totalTime').text(timeFormat(player.duration || 0));
			} catch (err) {
			
			}
		},
		rewind: function(src){
			this.player.currentTime -= TIME_FOR_SEEK;
		},
		foward: function(src){
			this.player.currentTime += TIME_FOR_SEEK;
		},
		play: function(){
			this.player.play();
		},
		pause: function(src){
			this.player.pause();
		},
		stop: function(src){
			try{
				this.player.pause();
				this.player.currentTime = 0;
			} catch (err) {
			
			}
		},
		channeling: function () {
			this.unbindEvent();
			this.playUrl = this.CHANNELING_URL;
			this.player.src = this.playUrl;
			this.player.play();
		},
		setSrc: function(src, isChanneling){
			this.bindEvent();
			$('#vod .bar b').css('width', '0%');
			$('#remainingTime').text(timeFormat(0));
			$('#totalTime').text(timeFormat(0));
			this.playUrl = src;
			this.player.src = this.playUrl;
			this.player.play();
		},
		rePlay: function() {
			this.player = this.getVideo();
			this.player.src = this.playUrl;
			this.player.play();
		}
	};
	
	
	var NETCASTVideo = {
		init: function(){
			this.player = this.getVideo();
			return this;
		},
		bindEvent: function(){
			var self = this;
			this.player.onPlayStateChange = function () {
				self.stateChange();
			};
			this.player.onBuffering = function () {
				self.buffering();
			};
		},
		unbindEvent: function(){
			this.player.onPlayStateChange = function () {};
			this.player.onBuffering = function () {};
		},
		getVideo: function(){
			var player;
			$('#videoHolder').html('<object type="application/x-netcast-av"  width="1280" height="720" id="video"></object>');
			player = $('#video')[0];
			return player;
		},
		removeVideo: function(){
			this.stop();
			$('#videoHolder').html('');
		},
		stateChange: function(){
			/*
				0				Stopped
				1				Playing
				2				Paused
				3				Connecting
				4				Buffering
				5				Finished
				6				Error
			*/
			if (this.player.playState == 0 || this.player.playState == 2 || this.player.playState == 5 || this.player.playState == 4) {
				this.resetProgress();
			} else if (this.player.playState == 1) {
				this.getVideoPlayInfo();
			} else if (this.player.playState == 6) {
				
			}
		},
		buffering: function () {
			// var self = this;
			// var videoPlayInfo = this.player.mediaPlayInfo();
			// if(videoPlayInfo)
			// {	
				 // if(videoPlayInfo.bufRemain!=-1){
					// var bufferPos= Math.ceil((videoPlayInfo.bufRemain/videoPlayInfo.duration)*progressBarWidth);
					// var pos=videoPlayInfo.duration-videoPlayInfo.bufRemain;
					// if(pos != 0)
					// {
						// pos=(pos/videoPlayInfo.duration)*progressBarWidth;
						// bufferPos+=pos;
						//setBufferPosition(bufferPos);
					// }
				 // }
			// }
			// if (this.player.playState == 4) {
				// setTimeout(function () {
					// self.buffering();
				// }, 100);
			// }
		},
		getVideoPlayInfo: function () {
			var self = this;
			//if ((this.player.playState != 5) && (this.player.playState != 0) && (this.player.playState != 2) ) {
				//$("#remainingTime").text(getTimeFromMS(this.player.currentPosition));
				//$("#totalTime").text(" / " + getTimeFromMS(this.player.duration));
				var pos = ((this.player.playPosition/1000)/(this.player.playTime / 1000))* 100;
				$('#vod .bar b').css('width', pos + '%');
				$('#remainingTime').text(timeFormat(this.player.playPosition ? (this.player.playPosition/1000) : 0));
				$('#totalTime').text(timeFormat(this.player.playTime ? (this.player.playTime/1000) : 0));
				this.playTimerId = setTimeout(function () {
					self.getVideoPlayInfo();
				}, 1000);
			//}
		},
		resetProgress: function (){
			clearTimeout(this.playTimerId);
			this.playTimerId = null;
		},
		rewind: function(src){
			//if (this.player.playState != 4 && this.player.playState == 1) {
				var fTime = parseInt(this.player.playPosition - (TIME_FOR_SEEK*1000));
				if (this.player.isSeekable) {
					if (fTime > 0) {
						this.player.seek(fTime);
					} else {
						this.player.seek(0);
					}
				}
			//} 
		},
		foward: function(src){
			//if (this.player.playState != 4 && this.player.playState == 1) {
			var fTime = parseInt(this.player.playPosition + (TIME_FOR_SEEK*1000));
				if (this.player.isSeekable) {
					if (fTime < this.player.playTime) {
						this.player.seek(fTime);
					} else {
						this.player.seek(this.player.playPosition);
					}
				}
			//} 
		},
		play: function(){
			this.player.play(1);
		},
		pause: function(){
			this.player.play(0);
			this.resetProgress();
		},
		stop: function(){
			if (this.player.playState != 0) {
				this.player.stop();
			}
		},
		channeling: function () {
			this.resetProgress();
			this.unbindEvent();
			this.playUrl = this.CHANNELING_URL;
			this.player.data = this.playUrl;
			this.player.play(1);
		},
		setSrc: function(url){
			this.bindEvent();
			$('#vod .bar b').css('width', '0%');
			$('#remainingTime').text(timeFormat(0));
			$('#totalTime').text(timeFormat(0));
			this.playUrl = url;
			this.player.data = this.playUrl;
			this.player.play(1);
		},
		rePlay: function() {
			this.player = this.getVideo();
			this.player.data = this.playUrl;
			this.player.play(1);
		}
	};
	
	if (!window.CJ) {
		window.CJ = {};
	}
	if (!window.CJ.plugin) {
		window.CJ.plugin = {};
	}
	CJ.plugin.Player = CJ.IS_TV ? NETCASTVideo.init() : HTML5Video.init();
	$(document).ready(function(){
		CJ.HttpJson.get({
			type: 'CHANNELING',
			success: function (json) {
				CJ.plugin.Player.CHANNELING_URL = json.url;
				CJ.plugin.Player.channeling();
			},
			error: function () {
				console.log('channeling url error');
			}
		});
	});
})();